//
//  GPVideoView.m
//  Keping
//
//  Created by 23 on 2017/12/10.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPVideoView.h"
#import "KLVideoCallButton.h"

@interface GPVideoView () <GPVideoToolViewDelegate>

/**语音通话时长label*/
@property(nonatomic,weak) IBOutlet UILabel *voiceTimeLabel;
/**底部工具栏*/
@property(nonatomic,weak) GPVideoToolView *toolView;
@property (weak, nonatomic) IBOutlet UIButton *resizeButton;

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *tipsLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconViewLeftConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconViewTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nameLabelLeftConstrain;

/**当前的状态*/
@property(nonatomic,assign) GPVideoViewStatus status;


@end

@implementation GPVideoView

+ (instancetype)videoView
{
    GPVideoView *videoView = nil;
    videoView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
    
    return videoView;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.toolView.frame = CGRectMake(0, self.bounds.size.height - 100, self.bounds.size.width, 100);
    
    NSLog(@"-----------> %@", NSStringFromCGRect(self.nameLabel.frame));
}


/**更新姓名和头像*/
- (void)updateUserInfoWithName:(NSString *)name avator:(UIImage *)avatorImage avatorPath:(NSString *)avatorPath
{
    self.nameLabel.text = name;
    if (avatorImage == nil) {
        avatorImage = [UIImage imageNamed:@"ic_common_holder_avatar"];
    }
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:avatorPath] placeholderImage:avatorImage];
}
/**更新语音通话时长*/
- (void)updateVoiceTimesecond:(double)time
{
    //转换成整数秒
    int second = (int)time;
    
    //根据剩余的秒数，计算所需要的小时数、分钟数和秒数
    second--;
    int hours = second / 3600;
    int last1 = second % 3600;
    int minutes = last1 / 60;
    int seconds = last1 % 60;
    
    
    if (hours == 0) {
        self.voiceTimeLabel.text = [NSString stringWithFormat:@"Time %02zd:%02zd", minutes, seconds];
    }else{
        self.voiceTimeLabel.text = [NSString stringWithFormat:@"Time %02zd:%02d:%02zd", hours, minutes, seconds];
    }
    
}

/**根据通话状态更新UI*/
- (void)updateUIWithStatus:(GPVideoViewStatus )status
{
    //保存当前的状态
    self.status = status;
    
    //根据状态切换UI
    switch (status) {
        case GPVideoViewStatusCalling:
        {
            self.tipsLabel.text = @"Awaiting response";
            self.tipsLabel.hidden = NO;
            self.voiceTimeLabel.text = @"";
            self.voiceTimeLabel.hidden = YES;
            self.iconView.hidden = NO;
            self.nameLabel.hidden = NO;
            self.resizeButton.hidden = YES;
        }
            break;
        
        case GPVideoViewStatusCalled:
        {
            self.tipsLabel.text = @"Incoming video call";
            self.tipsLabel.hidden = NO;
            self.voiceTimeLabel.text = @"";
            self.voiceTimeLabel.hidden = YES;
            self.iconView.hidden = NO;
            self.nameLabel.hidden = NO;
            self.resizeButton.hidden = YES;
        }
            break;
            
        case GPVideoViewStatusVideoSession:
        {
            self.tipsLabel.text = @"Video Conversation";
            self.tipsLabel.hidden = YES;
            self.voiceTimeLabel.text = @"Time 00:00";
            self.voiceTimeLabel.hidden = YES;
            self.resizeButton.hidden = NO;
            
            //隐藏icon+name
            self.iconView.hidden = YES;
            self.nameLabel.hidden = YES;
        }
            break;
            
        case GPVideoViewStatusVoiceSession:
        {
            self.tipsLabel.text = @"Voice Conversation";
            self.tipsLabel.hidden = YES;
            self.voiceTimeLabel.text = @"Time 00:00";
            self.voiceTimeLabel.hidden = NO;
            self.resizeButton.hidden = NO;
            
            //显示icon+name
            self.iconView.hidden = NO;
            self.nameLabel.hidden = NO;
        }
            break;
        
        default:
            break;
    }
    
    //如果是音频，需要将icon和name移至中间位置
    if(status == GPVideoViewStatusVoiceSession){
        //音频
        [self.iconView removeConstraints:self.iconView.constraints];
        for (NSLayoutConstraint *constraint in self.iconView.superview.constraints) {
            if ([constraint.firstItem isEqual:self.iconView]) {
                [self.iconView.superview removeConstraint:constraint];
            }
        }
        [self.nameLabel removeConstraints:self.nameLabel.constraints];
        for (NSLayoutConstraint *constraint in self.nameLabel.superview.constraints) {
            if ([constraint.firstItem isEqual:self.nameLabel]) {
                [self.nameLabel.superview removeConstraint:constraint];
            }
        }
        [self.tipsLabel removeConstraints:self.tipsLabel.constraints];
        for (NSLayoutConstraint *constraint in self.tipsLabel.superview.constraints) {
            if ([constraint.firstItem isEqual:self.tipsLabel]) {
                [self.tipsLabel.superview removeConstraint:constraint];
            }
        }
        
        [self.iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.centerY.mas_equalTo(self).offset(-60);
            make.width.mas_equalTo(73);
            make.height.mas_equalTo(77);
        }];
        
        [self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.iconView);
            make.top.mas_equalTo(self.iconView.mas_bottom).offset(5);
            make.width.mas_equalTo(200);
            make.height.mas_equalTo(30);
        }];
    }
    
    
    
    //移除工具栏
    if (self.toolView) {
        [self.toolView removeFromSuperview];
        self.toolView = nil;
    }
    
    //加载不同的工具栏
    if (status == GPVideoViewStatusCalling || status == GPVideoViewStatusCalled) {
        //主叫被叫+未接通
        GPVideoToolView *toolView = [GPVideoToolView toolViewWithStatus:GPVideoViewStatusCalled];
        toolView.delegate = self;
        toolView.frame = CGRectMake(0, self.bounds.size.height - 100, self.bounds.size.width, 100);
        [self addSubview:toolView];
        self.toolView = toolView;
    }else if(status == GPVideoViewStatusVideoSession){
        //视频音频+接通中
        GPVideoToolView *toolView = [GPVideoToolView toolViewWithStatus:GPVideoViewStatusVideoSession];
        toolView.delegate = self;
        toolView.frame = CGRectMake(0, self.bounds.size.height - 100, self.bounds.size.width, 100);
        [self addSubview:toolView];
        self.toolView = toolView;
    }else if (status == GPVideoViewStatusVoiceSession){
        GPVideoToolView *toolView = [GPVideoToolView toolViewWithStatus:GPVideoViewStatusVoiceSession];
        toolView.delegate = self;
        toolView.frame = CGRectMake(0, self.bounds.size.height - 100, self.bounds.size.width, 100);
        [self addSubview:toolView];
        self.toolView = toolView;
    }
    
    //切换工具栏的状态
    [self.toolView updateUIWithStatus:status];
}

#pragma mark - action
- (IBAction)resizeBtnClicked:(UIButton *)sender {
    
    sender.selected = !sender.isSelected;
    if (sender.isSelected) {
        //缩小
        self.toolView.hidden = YES;
    }else{
        //放大
        self.toolView.hidden = NO;
    }
    
    if ([self.delegate respondsToSelector:@selector(videoView:isMinisizeWindow:)]) {
        [self.delegate videoView:self isMinisizeWindow:sender.isSelected];
    }
}

//恢复UI
- (void)recoveryUI
{
    self.toolView.hidden = NO;
    self.resizeButton.selected = !self.resizeButton.selected;
}


#pragma mark - GPVideoToolViewDelegate
//被叫 -- 拒绝
- (void)toolView:(GPVideoToolView *)toolView status:(GPVideoViewStatus)status didRejectCalled:(KLVideoCallButton *)rejectButton
{
    if ([self.delegate respondsToSelector:@selector(videoView:didRejectBtnClicked:)]) {
        [self.delegate videoView:self didRejectBtnClicked:rejectButton];
    }
}

//被叫 -- 接听
- (void)toolView:(GPVideoToolView *)toolView status:(GPVideoViewStatus)status didReceiveCalled:(KLVideoCallButton *)receiveButton
{
    if ([self.delegate respondsToSelector:@selector(videoView:didReceiveBtnClicked:)]) {
        [self.delegate videoView:self didReceiveBtnClicked:receiveButton];
    }
}

//主叫 -- 取消
- (void)toolView:(GPVideoToolView *)toolView status:(GPVideoViewStatus)status didCancelCalling:(KLVideoCallButton *)cancelButton
{
    if ([self.delegate respondsToSelector:@selector(videoView:didCancelCalling:)]) {
        [self.delegate videoView:self didCancelCalling:cancelButton];
    }
}

//接通中 - 主动挂断
- (void)toolView:(GPVideoToolView *)toolView status:(GPVideoViewStatus)status didHangupSession:(KLVideoCallButton *)hangupButton
{
    if ([self.delegate respondsToSelector:@selector(videoView:didHangupSession:)]) {
        [self.delegate videoView:self didHangupSession:hangupButton];
    }
}

//接通中 - 切换摄像头
- (void)toolView:(GPVideoToolView *)toolView status:(GPVideoViewStatus)status didChangeCamera:(KLVideoCallButton *)changeButton
{
    if ([self.delegate respondsToSelector:@selector(videoView:didChangeCamera:)]) {
        [self.delegate videoView:self didChangeCamera:changeButton];
    }
}

//接通中 - 视频切换成语音
- (void)toolView:(GPVideoToolView *)toolView status:(GPVideoViewStatus)status didSwitchToVoice:(KLVideoCallButton *)switchButton
{
    if ([self.delegate respondsToSelector:@selector(videoView:didSwitchToVoice:)]) {
        [self.delegate videoView:self didSwitchToVoice:switchButton];
    }
}

//接通中 - 免提
- (void)toolView:(GPVideoToolView *)toolView status:(GPVideoViewStatus)status didHandsOff:(KLVideoCallButton *)handsOffButton ishandsOff:(BOOL)isHandsOff
{
    if ([self.delegate respondsToSelector:@selector(videoView:didHandsOff:)]) {
        [self.delegate videoView:self didHandsOff:isHandsOff];
    }
}

//接通中 - 静音
- (void)toolView:(GPVideoToolView *)toolView status:(GPVideoViewStatus)status didSlienceVoice:(KLVideoCallButton *)slienceButton isSlience:(BOOL)isSlience
{
    if ([self.delegate respondsToSelector:@selector(videoView:didSlienceVoice:)]) {
        [self.delegate videoView:self didSlienceVoice:isSlience];
    }
}





@end
